package com.byq.mm.smartlib.engine;

import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Looper;

import com.byq.mm.abstractx.cluster.client.event.ClusterChangeEvent;
import com.byq.mm.abstractx.event.CoreEventCenter;
import com.byq.mm.abstractx.protocol.Defination;
import com.byq.mm.common.event.EventCenter;
import com.byq.mm.smartlib.SmartActionConstants;
import com.byq.mm.smartlib.application.SmartApplication;


public class AndroidEventCenter implements EventCenter, CoreEventCenter {
	NotificationHandler notificationHandler;
	public AndroidEventCenter(Context ctx,NotificationHandler nh) {
		super();
		this.ctx = ctx;
		notificationHandler = nh;

	}


	Context ctx;

	@Override
	public void notifyLoginFail(Exception e) {
		notificationHandler.updateService(ctx, 0);
		Intent intent = new Intent(SmartActionConstants.LOGIN_FAIL);
		intent.putExtra("message", e.getLocalizedMessage());
		intent.setPackage(ctx.getPackageName());
		ctx.sendBroadcast(intent);
	}

	@Override
	public void notifyLoginSuccess() {
		notificationHandler.updateService(ctx, 2);
		Intent loggedIn = new Intent(SmartActionConstants.LOGIN_SUCCESS);
		loggedIn.setPackage(ctx.getPackageName());
		ctx.sendBroadcast(loggedIn);

	}
	@Override
	public void notifyLogout() {
		notificationHandler.updateService(ctx, 0);
		Intent loggedIn = new Intent(SmartActionConstants.LOGOUT);
		loggedIn.setPackage(ctx.getPackageName());
		ctx.sendBroadcast(loggedIn);

	}

	@Override
	public void notifyPresenceChange() {
		Intent intent = new Intent(SmartActionConstants.PRESENCE_CHANGED);
		intent.setPackage(ctx.getPackageName());
		ctx.sendBroadcast(intent);

	}

	@Override
	public void notifyDeviceChange() {
		Intent intent = new Intent(SmartActionConstants.DEVICE_CHANGED);
		intent.setPackage(ctx.getPackageName());
		ctx.sendBroadcast(intent);

	}

	public void notifyConnectionClosed() {
		notificationHandler.updateService(ctx, 0);
		Intent intent = new Intent(SmartActionConstants.CONNECTION_CLOSED);
		intent.setPackage(ctx.getPackageName());
		ctx.sendBroadcast(intent);
	}

	public void notifyConnectionClosedOnError(Exception e) {
		notificationHandler.updateService(ctx, 0);
		  

		SmartApplication app = (SmartApplication) ctx.getApplicationContext();

		app.getLoginManager().sendLogin();
		

		Intent intent = new Intent(SmartActionConstants.CONNECTION_ERROR_CLOSED);
		intent.setPackage(ctx.getPackageName());
		ctx.sendBroadcast(intent);
	}

	public void notifyReconnectingIn(int seconds) {
		// TODO Auto-generated method stub

	}

	public void notifyReconnectionFailed(Exception e) {
		// TODO Auto-generated method stub

	}

	public void notifyReconnectionSuccessful() {
		// TODO Auto-generated method stub

	}

	public void notifyInvite(Defination de) {
		notificationHandler.notifyInvite(de);
	}

	public void notifyAccept(String jid) {
		notificationHandler.notifyAccept(jid);
		
	}

	public void notifyRefuse(String jid) {
		notificationHandler.notifyRefuse(jid);
		
	}

	public void notifyUser(Map<String, String> msg) {
		notificationHandler.notifyUser(msg);
	
	}

	@Override
	public void notifyClusterChange(String deviceId, String clusterId, ClusterChangeEvent e) {
		Intent intent = new Intent(SmartActionConstants.CLUSTER_CLIENT_UPDATED);
		intent.setPackage(ctx.getPackageName());
		intent.putExtra("globalId", deviceId);
		intent.putExtra("clusterId", clusterId);
		intent.putExtra("event", e);
		ctx.sendBroadcast(intent);

	}

	@Override
	public void notifyLoginStart() {
		notificationHandler.updateService(ctx, 1);
	}
//
//	@Override
//	public void notifyFileReceive(IncomingFileTransfer in) {
//
//	}

	@Override
	public void notifyRequest(Defination de, String from) {
		notificationHandler.notifyRequest(de, from);
		
		
	}
	@Override
	public void notifyRequestRefuse(Defination de, String from) {
		notificationHandler.notifyRequestRefuse(de, from);
		
	}
	@Override
	public void notifyRequestAgree(Defination de, String from) {
		notificationHandler.notifyRequestAgree(de, from);
		
	}
	@Override
	public void notifyOfferAccepted(Defination de, String from) {
		notificationHandler.notifyOfferAccepted(de, from);
		
	}
	@Override
	public void notifyOfferRejected(Defination de, String from) {
		notificationHandler.notifyOfferRejected(de, from);

	}

	@Override
	public void notifyTransportClosed() {
		notificationHandler.notifyTransportClosed();

		
		
	}

	@Override
	public void notifyUnavailable(String to) {
		notificationHandler.notifyUnavailable(to);
		
		
	}

	@Override
	public void notifyAddContactRequest(String jid) {
		notificationHandler.notifyAddContactRequest(jid);
		
	}

	@Override
	public void notifyAddContactResonse(String jid, String type) {
		notificationHandler.notifyAddContactResonse(jid, type);
		
	}

	@Override
	public void loopPrepare() {
		Looper.prepare();
		
	}

}
